want to display a "generating image..." kind of modal dialog, other than the main GUI. This "generating image..." dialog should be temporary, and be displayed and disappear without user intervention.
For displaying this dialog, the Qt code should check for existence of a .txt file in a specific location in the PC's hard disk. If the .txt file exists, then the dialog should pop-up.
For making this dialog disappear, the Qt code should check whether that .txt file contains the string "OK" in the first line. The dialog should disappear only when this "OK" is found, until then it should continue to display "generating image..."
A good way to do this is to use signal slot mechanism. I would like to know, what functions should be used as SIGNALS in both the cases, of displaying and removing the dialog.
So far, I could manage a simple code, illustrating a "generating image..." using signal slot mechanism, but with setValue() and pressing a push button(i.e. involving user intervention), and not with the checking of .txt file or the "OK" string inside that .txt file(user non-intervention).
Please advise me, whether my logic can be implemented? If yes, how? Also, what SIGNALs should be used?
Here is my code:
dialog.h
#ifndef DIALOG_H
#define DIALOG_H
#include <QDialog>
#include <QProgressDialog>
#include "mytask.h"
namespace Ui {
class Dialog;
}
{
Q_OBJECT
public:
explicit Dialog
(QWidget *parent
= 0);
~Dialog();
private slots:
void on_modalButton_clicked();
private:
Ui::Dialog *ui;
MyTask *myTask;
};
#endif // DIALOG_H
#ifndef DIALOG_H
#define DIALOG_H
#include <QDialog>
#include <QProgressDialog>
#include "mytask.h"
namespace Ui {
class Dialog;
}
class Dialog : public QDialog
{
Q_OBJECT
public:
explicit Dialog(QWidget *parent = 0);
~Dialog();
private slots:
void on_modalButton_clicked();
private:
Ui::Dialog *ui;
MyTask *myTask;
};
#endif // DIALOG_H
To copy to clipboard, switch view to plain text mode
mytask.h
#ifndef MYTASK_H
#define MYTASK_H
#include <QObject>
#include <QProgressDialog>
#include <QTimer>
#include <QDebug>
#define NUM_TASKS 50000
{
Q_OBJECT
public:
explicit MyTask
(QObject *parent
= 0);
signals:
public slots:
void perform();
void cancel();
private:
int steps;
};
#endif // MYTASK_H
#ifndef MYTASK_H
#define MYTASK_H
#include <QObject>
#include <QProgressDialog>
#include <QTimer>
#include <QDebug>
#define NUM_TASKS 50000
class MyTask : public QObject
{
Q_OBJECT
public:
explicit MyTask(QObject *parent = 0);
signals:
public slots:
void perform();
void cancel();
private:
int steps;
QProgressDialog *pd;
QTimer *t;
};
#endif // MYTASK_H
To copy to clipboard, switch view to plain text mode
dialog.cpp
#include "dialog.h"
#include "ui_dialog.h"
ui(new Ui::Dialog)
{
ui->setupUi(this);
}
Dialog::~Dialog()
{
delete ui;
}
void Dialog::on_modalButton_clicked()
{
myTask = new MyTask;
}
#include "dialog.h"
#include "ui_dialog.h"
Dialog::Dialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::Dialog)
{
ui->setupUi(this);
}
Dialog::~Dialog()
{
delete ui;
}
void Dialog::on_modalButton_clicked()
{
myTask = new MyTask;
}
To copy to clipboard, switch view to plain text mode
mytask.cpp
#include "mytask.h"
{
connect(pd, SIGNAL(canceled()), this, SLOT(cancel()));
connect(t, SIGNAL(timeout()), this, SLOT(perform()));
t->start(0);
/* to make a window modal in nature. */
pd->setModal(true);
}
void MyTask::perform()
{
disconnect(pd, SIGNAL(canceled()), this, SLOT(cancel()));
pd->setValue(steps); //... perform one percent of the operation
steps++;
qDebug()<<steps;
if (steps > pd->maximum())
{
t->stop();
}
}
void MyTask::cancel()
{
t->stop(); //... cleanup
}
#include "mytask.h"
MyTask::MyTask(QObject *parent) :
QObject(parent), steps(0)
{
pd = new QProgressDialog("generating image...", 0, 0, NUM_TASKS);
connect(pd, SIGNAL(canceled()), this, SLOT(cancel()));
t = new QTimer(this);
connect(t, SIGNAL(timeout()), this, SLOT(perform()));
t->start(0);
/* to make a window modal in nature. */
pd->setModal(true);
}
void MyTask::perform()
{
disconnect(pd, SIGNAL(canceled()), this, SLOT(cancel()));
pd->setValue(steps); //... perform one percent of the operation
steps++;
qDebug()<<steps;
if (steps > pd->maximum())
{
t->stop();
}
}
void MyTask::cancel()
{
t->stop(); //... cleanup
}
To copy to clipboard, switch view to plain text mode
main.cpp
#include "dialog.h"
#include <QApplication>
int main(int argc, char *argv[])
{
Dialog w;
w.show();
return a.exec();
}
#include "dialog.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Dialog w;
w.show();
return a.exec();
}
To copy to clipboard, switch view to plain text mode
Bookmarks